var pomelo = window.pomelo;

var host = '127.0.0.1', gatePort = 3014, connPort = 3050;

if (location.protocol === 'file:') {
    alert('请勿使用文件方式打开！');
    log('请勿使用文件方式打开！');
}

var params = location.search.split('?')[1].split('_');
var gameId = 4,
    playerId = params[1] || 1004,
    token = params[2];

var router = {
    enterGame: 'connector.entryHandler.enterGame'
};

var events = {
    onPlayerEnter: 'onPlayerEnter',
    onPlayerLeave: 'onPlayerLeave',

    onGameXiaZhu: 'onGameXiaZhu',
    onGameKaiPai: 'onGameKaiPai',
    onGameFree: 'onGameFree',
    onGameStart: 'onGameStart',
    onGameBid: 'onGameBid',
    onGameOver: 'onGameOver',
    onCalcResult: 'onCalcResult',
    onCancelBanker: 'onCancelBanker',
    onApplyBanker: 'onApplyBanker',
    onCutInBanker: 'onCutInBanker',
    onSetBanker: 'onSetBanker'
};

pomelo.init(
    {
        host: '127.0.0.1',
        port: connPort,
        log: true
    },
    function () {
        var route = router.enterGame;
        pomelo.request(
            route,
            {
                playerId,
                token,
                gameId
            },
            function (data) {
                // log(data);
                if (data.err) {
                    log('用户进入游戏失败：', data.err);
                }
                else {
                    log('用户进入游戏正常！');
                }
            });
    });

pomelo.on(events.onPlayerEnter, function (data) {
    var eve = 'onPlayerEnter';
    var msg = `playerId: ${data.playerId}, name:  ${data.name}, coin:  ${data.coin}, roundCount:  ${data.roundCount}, score:  ${data.score}`;
    bold(eve, msg);
});

pomelo.on(events.onPlayerLeave, function (data) {
    var eve = 'onPlayerLeave';
    var msg = `playerId: ${data.playerId}`;
    bold(eve, msg);
});

pomelo.on(events.onGameFree, function (data) {
    var eve = 'onGameFree';
    var msg = `checkTime: ${data.checkTime}, gameStatus: ${data.gameStatus}`;
    bold(eve, msg);
    log(data);
});

pomelo.on(events.onGameStart, function (data) {
    var eve = 'onGameStart';
    var msg = `checkTime: ${data.checkTime}, gameStatus: ${data.gameStatus}`;
    bold(eve, msg);
});

pomelo.on(events.onGameXiaZhu, function (data) {
    var eve = 'onGameXiaZhu';
    var msg = `checkTime: ${data.checkTime}, gameStatus: ${data.gameStatus}`;
    bold(eve, msg);
});

pomelo.on(events.onGameKaiPai, function (data) {
    var eve = 'onGameKaiPai';
    var msg = `checkTime: ${data.checkTime}, gameStatus: ${data.gameStatus}`;
    bold(eve, msg);
    log(data);
});


pomelo.on(events.onGameBid, function (data) {
    var msg = `事件<onGameBid>: playerId: ${data.playerId}`;
    log(msg, data);
});

pomelo.on(events.onGameOver, function (data) {
    var eve = 'onGameOver';
    var msg = `ret: ${data.ret}`;
    bold(eve, msg);
});

pomelo.on(events.onCalcResult, function (data) {
    var eve = 'onGameOver';
    var msg = `checkTime: ${data.checkTime}, gameStatus: ${data.gameStatus}`;
    bold(eve, msg);
    log(data);
});

pomelo.on(events.onCancelBanker, function (data) {
    var msg = `事件<onCancelBanker>: playerId: ${data.playerId}`;
    log(msg, data);
});

// console.log('%c123', 'font-weight: bold');
// bold(123);